package com.hs.sfzy.service.impl;

import com.hs.sfzy.entity.ClassQuestion;
import com.hs.sfzy.entity.User;
import com.hs.sfzy.mapper.ClassAnswerMapper;
import com.hs.sfzy.mapper.ClassQuestionMapper;
import com.hs.sfzy.mapper.UserMapper;
import com.hs.sfzy.service.ClassQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName ClassQuestionServiceImpl
 * @Description TODO
 * @Author Chen
 * @Date 2025年08月13日 11:13
 * @Version 1.0
 */
@Service
public class ClassQuestionServiceImpl implements ClassQuestionService {
    @Autowired
    private ClassQuestionMapper classQuestionMapper;
    @Autowired
    private ClassAnswerMapper classAnswerMapper;

    @Override
    public List<ClassQuestion> getAllClassQuestion() {
        List<ClassQuestion> allClassQuestion = classQuestionMapper.getAllClassQuestion();
        for (ClassQuestion classQuestion : allClassQuestion) {
            Integer id = classQuestion.getId();
            Integer counts = classAnswerMapper.getClassAnswerCountByQuestionId(id);
            classQuestion.setCount(counts);
            classQuestionMapper.updateClassQuestion(classQuestion);
        }
        return allClassQuestion;
    }

    @Override
    public List<ClassQuestion> getClassQuestionList(Integer userId) {
        return classQuestionMapper.getClassQuestionList(userId);
    }

    @Override
    public Integer addClassQuestion(ClassQuestion classQuestion) {
        return classQuestionMapper.addClassQuestion(classQuestion);
    }
@Autowired
private UserMapper  userMapper;
    @Override
    public ClassQuestion getClassQuestion(Integer id) {
        ClassQuestion classQuestion = classQuestionMapper.getClassQuestion(id);
        Integer userId = classQuestion.getUserId();
        User userById = userMapper.findUserById(userId);
        classQuestion.setUser(userById);
        return classQuestion;
    }
}
